package it.polimi.traveldream.ejb.eaos;

import java.util.ArrayList;
import java.util.List;

import it.polimi.traveldream.ejb.dto.ProviderDTO;
import it.polimi.traveldream.ejb.entities.Provider;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class ProviderManager
 */
@Stateless
public class ProviderManager implements ProviderManagerLocal {

	@PersistenceContext
    private EntityManager em;
	
	@Resource
	private EJBContext context;
	
	
    public ProviderManager() {
        System.out.println("provide creato");
    }

	@Override
	@RolesAllowed({"EMPLOYEE"})
	public void AddProvider(ProviderDTO provider) {
		em.persist(new Provider(provider));	
	}

	@Override
	@RolesAllowed({"EMPLOYEE"})
	public void RemoveProvider(String providerName) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	@RolesAllowed({"EMPLOYEE"})
	public List<ProviderDTO> getAllProvider(){
		System.out.println("query chiamata");
		Query q = em.createQuery ("SELECT x FROM Provider x");
		List<Provider> results =(List<Provider>) q.getResultList ();
		List<ProviderDTO> dtol = new ArrayList<ProviderDTO>();
		for(Provider p: results){
			dtol.add(p.getDTO());
		}
		System.out.println("query terminata");
		return dtol;
	}
}
